package com.Hsu.demo.controller;

import com.Hsu.demo.config.UserInfo;
import com.Hsu.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * @author Hsu琛君珩
 * @ClassName:UserController
 * @date 2023-12-03
 * @apiNote
 * @Version: v1.0
 */

@Controller//将对象存储到 Spring 中
public class UserController {

    //属性注入
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfo user1;
    //使用@Qualifier指定bean
    //@Qualifier("userInfo2")
    //@Autowired
    //private UserInfo userInfo;

    //使用@Resource指定bean
    //@Resource(name="userInfo2")
    //private UserInfo userInfo;

    //构造方法注入
    //private UserService userService;
    //private UserInfo userInfo;

    //Spring会默认优先使用无参构造函数，这样就没有userService对象，下面doController()方法就空指针异常了
    //public UserController() {
    //}

    //注释了无参构造，下面两个构造函数Spring就不知道用哪个了，因为Spring是默认优先使用无参的
    //因此我们要显式告诉Spring用哪个，这里就用到@Autowired
    //@Autowired
    //public UserController(UserService userService) {
    //    this.userService=userService;
    //}
    //public UserController(UserService userService, UserInfo userInfo) {
    //    this.userService = userService;
    //    this.userInfo = userInfo;
    //}

    //Setter方法注入
    //private UserService userService;

    //@Autowired
    //public void setUserService(UserService userService) {
    //    this.userService = userService;
    //}

    public void doController(){
        userService.doService();
        System.out.println(user1);
        System.out.println("doController...");
    }
}
